<#include "../../ajaxPage.ftl" />
<!-- Modal -->
<div class="modal-dialog wide" style="width: 550px;">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="myModalLabel">分配角色</h4>
        </div>
        <div class="modal-body" style="height: 400px; overflow-y: auto">
            <div id="errorMessage"><#include "../../message.ftl" /></div>
            <form class="form-horizontal" role="form" id="search_assign_role_table" method="post"
                  action="${absoluteContextPath}/system/role/goAssignAuth">
                <input type="hidden" name="userId" value="${userId!''}"/>
                <table class="table table-datatable table-gray check_table">
                    <thead>
                    <tr>
                        <th style="width: 35px;"><input id="checkRoleIdAll" type="checkbox" value=""></th>
                        <th>角色ID</th>
                        <th>角色名称</th>
                    </tr>
                    </thead>
                    <tbody>
                    <#if roleList??>
                        <#list roleList as role>
                        <tr>
                            <td>
                                <#if assignRoleList?seq_contains(role.roleId!'')>
                                    <input name="roleIds" type="checkbox" value="${role.roleId!''}" checked="checked">
                                <#else>
                                    <input name="roleIds" type="checkbox" value="${role.roleId!''}">
                                </#if>
                            </td>
                            <td>${role.roleId!''}</td>
                            <td>${role.roleName!''}</td>
                        </tr>
                        </#list>
                    </#if>
                    </tbody>
                </table>
            </form>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            <button type="button" class="btn btn-primary" onclick="save();">保存</button>
        </div>
    </div>
</div>
<script>

    $(function () {
        $("#checkRoleIdAll").change(function () {
            if ($(this).attr("checked")) {
                $("input[name=roleIds]").attr("checked", true);
            } else {
                $("input[name=roleIds]").attr("checked", false);
            }
        });
        $("input[name=roleIds]").change(function () {
            if (!$(this).attr("checked")) {
                $("#checkAll").attr("checked", false);
            }
        });
    });

    function save() {
        $("#" + "search_assign_role_table").ajaxSubmit({
            url: '${absoluteContextPath}/system/user/assignRole',
            success: function (data) {
                data = JSON.parse(data);
                if (data.returnCode == 1) {
                    $.showMessage({parentId: "errorMessage", message: "信息保存成功！", type: "success", mode: "modal"});
                } else {
                    $.showMessage({parentId: "errorMessage", message: "<strong>错误信息</strong>：" + data.returnMsg, type: "success", mode: "modal"});
                }
            }
        });
    }
</script>